document.addEventListener('DOMContentLoaded', () => {
  // 初始化提醒类型切换
  const typeRadios = document.querySelectorAll('input[name="type"]');
  const fixedTimeSettings = document.getElementById('fixedTimeSettings');
  const intervalSettings = document.getElementById('intervalSettings');
  
  typeRadios.forEach(radio => {
    radio.addEventListener('change', (e) => {
      if (e.target.value === 'fixed') {
        fixedTimeSettings.classList.remove('hidden');
        intervalSettings.classList.add('hidden');
      } else {
        fixedTimeSettings.classList.add('hidden');
        intervalSettings.classList.remove('hidden');
      }
    });
  });

  // 添加提醒
  document.getElementById('addReminder').addEventListener('click', async () => {
    const text = document.getElementById('reminderText').value;
    const type = document.querySelector('input[name="type"]:checked')?.value;
    
    if (!text || !type) {
      alert('请输入提醒内容并选择提醒类型');
      return;
    }
    
    const reminder = {
      id: Date.now(),
      text,
      type,
      completed: false,
      createdAt: new Date()
    };
    
    if (type === 'fixed') {
      reminder.time = document.getElementById('fixedTime').value;
      if (!reminder.time) {
        alert('请选择提醒时间');
        return;
      }
    } else {
      reminder.interval = parseInt(document.getElementById('interval').value);
      reminder.quietStart = document.getElementById('quietStart').value;
      reminder.quietEnd = document.getElementById('quietEnd').value;
      reminder.maxDaily = parseInt(document.getElementById('maxDaily').value) || null;
      reminder.todayCount = 0;
      
      if (!reminder.interval) {
        alert('请填写提醒间隔时间');
        return;
      }
      
      if ((reminder.quietStart && !reminder.quietEnd) || (!reminder.quietStart && reminder.quietEnd)) {
        alert('如果设置免打扰时间，开始和结束时间都需要填写');
        return;
      }
    }
    
    const data = await chrome.storage.local.get('reminders');
    const reminders = data.reminders || [];
    reminders.push(reminder);
    await chrome.storage.local.set({ reminders });
    await loadReminders();
    resetForm();
  });

  // 加载提醒列表
  loadReminders();
});

async function loadReminders() {
  const data = await chrome.storage.local.get('reminders');
  const reminders = data.reminders || [];
  const list = document.getElementById('remindersList');
  list.innerHTML = '';
  
  reminders.forEach(reminder => {
    const li = document.createElement('li');
    li.innerHTML = `
      <input type="checkbox" ${reminder.completed ? 'checked' : ''}>
      <span class="${reminder.completed ? 'completed' : ''}">${reminder.text}</span>
      <span class="reminder-info">
        ${reminder.type === 'fixed' ? 
          `定点: ${new Date(reminder.time).toLocaleString()}` : 
          `间隔: ${reminder.interval}分钟`}
        ${reminder.quietStart && reminder.quietEnd ? 
          `<br>免打扰: ${reminder.quietStart}-${reminder.quietEnd}` : ''}
        ${reminder.maxDaily ? 
          `<br>每日上限: ${reminder.todayCount || 0}/${reminder.maxDaily}` : ''}
      </span>
    `;
    
    const checkbox = li.querySelector('input[type="checkbox"]');
    checkbox.addEventListener('change', async () => {
      if (checkbox.checked) {
        const data = await chrome.storage.local.get('reminders');
        const reminders = data.reminders || [];
        const updatedReminders = reminders.map(r => {
          if (r.id === reminder.id) {
            return { ...r, completed: true, completedAt: new Date() };
          }
          return r;
        });
        await chrome.storage.local.set({ reminders: updatedReminders });
        await loadReminders();
      }
    });
    
    list.appendChild(li);
  });
}

function resetForm() {
  document.getElementById('reminderText').value = '';
  document.getElementById('fixedTime').value = '';
  document.getElementById('interval').value = '';
  document.getElementById('quietStart').value = '';
  document.getElementById('quietEnd').value = '';
  document.getElementById('maxDaily').value = '';
} 